package com.hardroid.workload.ui.fragment;

import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.TextView;

import com.hardroid.workload.R;
import com.hardroid.workload.ui.BebanActivity;
import com.hardroid.workload.utils.Singleton;
import com.hardroid.workload.utils.UnitAdapter;
import com.hardroid.workload.workcalc.ObsvProject.ProjectStatus;

/**
 * Isinya daftar unit kerja yang ada di project
 * @author hartono
 *
 */
public class UnitKerjaFragment extends ListFragment {

	public UnitAdapter adapter;
	public final static int REMOVE_UNIT = 0;	
	public int selectionPos = -999;

	public TextView idText;
	public TextView nameText;	
	public TextView unitText;
	public TextView jobText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		try {
			// if (Singleton.currentProject.lEmployer.size()!=0){
			// if (Singleton.employerAdapter==null){
			
			adapter = Singleton.unitAdapter;

			setListAdapter(adapter);

			// }else{			
			// }

			// }
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_unit_kerja, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		idText = (TextView) getView().findViewById(R.id.list_item_unit_id);
		nameText = (TextView) getView().findViewById(
				R.id.list_item_unit_name);		
		unitText = (TextView) getView().findViewById(
				R.id.list_item_unit_unit);
		jobText = (TextView) getView()
				.findViewById(R.id.list_item_unit_job);

		
		
		idText.setText("No");
		nameText.setText("Name");		
		unitText.setText("Unit");
		jobText.setText("Pekerjaan");


		registerForContextMenu(getListView());
	}

	public void removeUnit() {

		Singleton.currentProject.removeUnit(selectionPos);

		// commit
		Singleton.saveProject(Singleton.currentProject);
		adapter.notifyDataSetChanged();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		if (Singleton.currentProject.projectStatus!=ProjectStatus.PROJECT_FINISHED){
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
			menu.setHeaderTitle("Menu");
			menu.add(0, REMOVE_UNIT, 0, "Remove");
			selectionPos = info.position;			
		}
		
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if (item.getItemId() == REMOVE_UNIT) {
			removeUnit();
			return true;
		}
		return false;
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		l.setItemChecked(position, true);
		selectionPos = position;
		openQuestionaire();
	}
	
	public void openQuestionaire(){
		Intent i = new Intent(getActivity(), BebanActivity.class);
		i.putExtra("id", selectionPos);
		startActivity(i);
		
	}
	

}
